#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################


rm(list=ls())

source("C:/UQAR/Recherche/Maitrise/Donnees/R/Utils/loadDependencies.R")

MOVEBANK_DIR <- "C:/UQAR/Recherche/Maitrise/Donnees/Movebank"

#'#################################################################################################
#'#                                                                                              ##
#'#                                 Recuperation des donnees                                     ##
#'#                                                                                              ##
#'#################################################################################################


#'#
#'# Donnees statiques
#'#

STATIC_WORKING_DIR <- "C:/UQAR/Recherche/Maitrise/Donnees/Bylot/Fixe"
STATIC_DEST_DIR <- file.path(MOVEBANK_DIR, "Statique")

visibilities <- getStaticVisibilities(STATIC_WORKING_DIR)
static.data <- extractStaticData(visibilities, argoscolumns=c("Jour", "Platform.ID.", 
				"Platform", "Prg.No.", "Latitude", "Longitude", "Loc..quality", "Loc..date",
				"Sat.", "Pass", "Altitude", "Frequency", "Msg.Date", "Comp.", "Long..1",
				"Lat..sol..1", "Long..2", "Lat..sol..2", "Loc..idx", "Nopc", "Msg",
				"X.gt....120.DB", "Best.level", "Delta.freq.", "Error.radius", "Semi.major.axis",
				"Semi.minor.axis", "Ellipse.orientation", "GDOP", "Heure"))

static.error <- lapply(static.data, getError)
static.errors <- rbindlist(lapply(static.error, function(x) {return(x@data)}))
setkey(static.errors, visibility, LC)

static.errors[, deployment := paste(visibility, session, sep="_") ]
static.errors[, error := paste(diffLong, diffLat, sep=";")]


write.table(static.errors, file.path(STATIC_DEST_DIR, "Argos_static.txt"), row.names=FALSE, sep="\t")

		

#'#
#'# Donnees mobiles
#'#

MOBILE_WORKING_DIR <- "C:/UQAR/Recherche/Maitrise/Donnees/Bylot/Mobile"
MOBILE_DEST_DIR <- file.path(MOVEBANK_DIR, "Mobile")

mobile.errors <- loadData(MOBILE_WORKING_DIR, static=FALSE, filtered=FALSE,
				argoscolumns=c("Jour", "Platform.ID.", 
						"Platform", "Prg.No.", "Latitude", "Longitude", "Loc..quality", "Loc..date",
						"Sat.", "Pass", "Altitude", "Frequency", "Msg.Date", "Comp.", "Long..1",
						"Lat..sol..1", "Long..2", "Lat..sol..2", "Loc..idx", "Nopc", "Msg",
						"X.gt....120.DB", "Best.level", "Delta.freq.", "Error.radius", "Semi.major.axis",
						"Semi.minor.axis", "Ellipse.orientation", "GDOP", "Heure"))

#mobile.errors[, deployment := paste(jour, gpsId, sep="_") ]
mobile.errors[, error := paste(diffLong, diffLat, sep=";")]

write.table(mobile.errors, file.path(MOBILE_DEST_DIR, "Argos_mobile2.txt"), row.names=FALSE, sep="\t")


#'#
#'# Donnees de deploiement
#'# 

dataPath <- "C:/UQAR/Recherche/Maitrise/Donnees"
sessionInfoFile <- "Prise_donnees_2012.csv"
infoDateFormat <- "%d/%m/%y"

sessionInfo <- data.table(parseSessionInfo(file.path(dataPath, sessionInfoFile), infoDateFormat))
setkey(sessionInfo, Type)
mobile.deployment <- sessionInfo["M"]
#mobile.deployment[, deployment := paste(Date, "_GPS", GPS, sep="")]
mobile.deployment[, Date_debut := paste(Date, "00:00:00")]
mobile.deployment[, Date_fin := paste(Date, "23:59:59")]

write.table(mobile.deployment, file.path(MOBILE_DEST_DIR, "Argos_mobile_deployment2.txt"), row.names=FALSE, sep="\t")


